// pages/mod/sou_suo.js
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data:{
		sku_click:0,
		point_num:0,
		total_price:0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that=this;
		//获取购物车列表
		app.request(app.apiDomain + "index.php?c=cart&a=cartList", {}, function(res) {
			// console.log(res.data.data)
			var clist = res.data.data.list;
			for(var i=0; i < clist.length; i++){
				var a = clist[i].sku;
				var b = clist[i];
				var c = b.min_price ? b.min_price:b.sku_list[a][5];
				b.sku_name = [];
				b.money = ((c ? c:0) * b.num).toFixed(1);
				b.bi = (b.sku_list[a][2] * b.num).toFixed(1);
				for(var r=0; r < b.sku_list.length; r++){
					b.sku_name.push(b.sku_list[r][0]);
				};
			}
			// console.log(clist)
			that.setData({
				cartList:clist,
			});
		});
	},
	li_click:function(e){
		var data = this.data.cartList;
		if(e){
			var num = e.currentTarget.dataset.num;
			data[num].on = data[num].on ? false:1;
		}else{
			var cart_id_list = '';
			for(var i=0; i<data.length;i++){
				data[i].on = 1;
			};
		}
		
		this.setData({
			cartList:data,
		});
		//
		this.money_count()
		// console.log(this.data.cartList)
	},
	money_count:function(){
		var that = this;
		var data = this.data.cartList;
		var cart_id_list = '';
		for(var i=0; i<data.length;i++){
			if(data[i].on){
				cart_id_list += data[i].cart_id + ','
			};
		};
		cart_id_list = cart_id_list.substring(0,cart_id_list.length-1)
		//计算价格
		app.request(app.apiDomain + "index.php?c=cart&a=preview", {
			cart_id_list:cart_id_list
		}, function(res) {
			console.log(res.data.data)
			that.setData({
				point_num:res.data.data.point_num,
				total_price:res.data.data.total_price
			});
		});
		
	},
	whole_click:function(){
		this.li_click(false,true)
	},
	bindPickerChange:function(e){
		var i = e.currentTarget.dataset.num;
		var b = this.data.cartList[i];
		var v = e.detail.value;
		var c = b.min_price ? b.min_price:b.sku_list[a][5];
		this.setData({
			['cartList['+i+'].sku']:v,
			['cartList['+i+'].money']:(c ? c:0 * b.num).toFixed(1),
			['cartList['+i+'].bi']:(b.sku_list[v][2] * b.num).toFixed(1),
		})
		var wrds = {};
		wrds.id = e.currentTarget.dataset.id;
		wrds.sku = v;
		wrds.num = e.currentTarget.dataset.num;
		this.CartModify(wrds);
	},
	num_le:function(e){
		var i = e.currentTarget.dataset.num;
		var b = this.data.cartList[i];
		var a =  parseFloat(b.num) - 1;
		var v =  b.sku;
		if(a <= 0){
			return false
		};
		this.setData({
			['cartList['+i+'].num']:a,
			['cartList['+i+'].money']:(b.sku_list[v][5] * a).toFixed(1),
			['cartList['+i+'].bi']:(b.sku_list[v][2] * a).toFixed(1),
		});
		var wrds = {};
		wrds.id = e.currentTarget.dataset.id;
		wrds.sku = e.currentTarget.dataset.sku;
		wrds.num = a;
		this.CartModify(wrds);
	},
	num_ri:function(e){
		var i = e.currentTarget.dataset.num;
		var b = this.data.cartList[i];
		var a =  parseFloat(b.num) + 1;
		var v =  b.sku;
		if(a > b.left_num){
			wx.showToast({
				title: '库存不足',
				icon: 'none',
				duration: 2000
			})
			return false
		};
		this.setData({
			['cartList['+i+'].num']:a,
			['cartList['+i+'].money']:(b.sku_list[v][5] * a).toFixed(1),
			['cartList['+i+'].bi']:(b.sku_list[v][2] * a).toFixed(1),
		})
		var wrds = {};
		wrds.id = e.currentTarget.dataset.id;
		wrds.sku = e.currentTarget.dataset.sku;
		wrds.num = a;
		this.CartModify(wrds);
	},
	// 购物车修改
	CartModify:function(e){
		app.request(app.apiDomain + "index.php?c=cart&a=cartEdit", {
			cart_id:e.id,
			sku:e.sku,
			num:e.num,
		}, function(res) {
			console.log(res.data.info)
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
